package com.atguigu.yygh.user.controller.user;


import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.result.R;
import com.atguigu.yygh.user.service.PatientService;
import com.atguigu.yygh.utils.JwtHelper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 就诊人表 前端控制器
 * </p>
 *
 * @author atguigu
 * @since 2022-04-24
 */
@RestController
@RequestMapping("/api/userinfo/patient")
public class PatientController {


    @Autowired
    private PatientService patientService;

    @PostMapping("/save")
    public R save(@RequestBody  Patient patient, @RequestHeader String token){
        Long userId = JwtHelper.getUserId(token);
        patient.setUserId(userId);
        patientService.save(patient);
        return R.ok();
    }

    @DeleteMapping("/delete/{pid}")
    public R save(@PathVariable Long pid){
       patientService.removeById(pid);
       return R.ok();
    }
    //根据就诊人id查询就诊人信息
    @GetMapping("/info/{pid}")
    public R getPatientInfo(@PathVariable Long pid){
        Patient patient =patientService.selectById(pid);
        return R.ok().data("patient",patient);
    }

    //根据就诊人id获取就诊人信息，用于远程调用
    @GetMapping("/remote/{pid}")
    public Patient getRemotePatientInfo(@PathVariable("pid") Long pid){
        return patientService.selectById(pid);
    }



    //修改就诊人信息
    @PutMapping("/update")
    public R update(@RequestBody Patient patient){
        patientService.updateById(patient);
        return R.ok();
    }

    //查询当前登录用户底下所有的就诊人信息
    @GetMapping("/patients")
    public R getPatientsInfo(@RequestHeader String token){
        Long userId = JwtHelper.getUserId(token);
        QueryWrapper<Patient> queryWrapper=new QueryWrapper<Patient>();
        queryWrapper.eq("user_id", userId);
        List<Patient> list = patientService.selectList(queryWrapper);
        return R.ok().data("list",list);
    }

}

